import { ref, reactive, onMounted } from 'vue';
import request from '@/utils/request';
import { codeToText } from "element-china-area-data";
import { getClassifyAllList } from '@/api/goods/classify/index';

export default function ListMixin(_this){
        const dataSource = ref([]); // 列表数据
        const multiple = ref(false); // 是否多选
        const loading = ref(false); // 加载状态
        const showSearch = ref(true); // 是否显示查询条件
        const queryParams = reactive({ // 查询条件
            pageNum: 1,
            pageSize: 10
        });
        const total = ref(0); // 总数
        const selectedRows = ref([]); // 选中的行
        const categoryList = ref([]); // 商品分类列表
        const classifyList = ref([]); // 供应商或客户分类
        const dateRange = ref([]); // 商品分类列表
        const updateDateRange = ref([]); // 供应商或客户分类

        const { proxy } = getCurrentInstance();
        const codeToText = (code) => {
            if (code && code.length > 0 && code.split(',').length > 1) {
                let arr = code.split(',');
                return arr.map(item => {
                    return codeToText[item];
                }).join('/');
            }
        };

        const handleAdd = (row) => {
            console.log('proxy?.$refs :>> ', proxy?.$refs);
            proxy?.$refs.modelForm.add(row);
        };

        const handleEdit = (row) => {
            proxy?.$refs.modelForm.edit(row);
        };

        const handleQuery = () => {
            queryParams.pageNum = 1;
            getList();
        };

        const resetQuery = () => {
            queryParams.pageNum = 1;
            queryParams.pageSize = 10;
            getList();
        };

        const getList = () => {
            loading.value = true;
            var params = {
                url: _this.urls.list,
                method: 'get',
                params:getQueryParams() // 查询条件
            };

            request(params).then(res => {
                loading.value = false;
                if (res.code == 200) {
                    dataSource.value = res.rows;
                    total.value = res.total;
                } else {
                    $message.error(res.msg);
                }
            });
        };

        const getQueryParams = () => {
            if (typeof _this.getQueryParams === 'function') {
                return _this.getQueryParams();
            }
            return queryParams;
        };

        const handleDelete = (ids) => {
            proxy?.$modal.confirm('确定删除？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                request({
                    url: _this.urls.delete + '/' + ids,
                    method: 'delete'
                }).then(res => {
                    if (res.code == 200) {
                        proxy?.$modal.msgSuccess('删除成功');
                        getList();
                    } else {
                        $message.error(res.msg);
                    }
                });
            }).catch(() => {});
        };

        const selectHandle = (row, index) => {
            return true;
        };

        const handleSelectionChange = (val) => {
            selectedRows.value = val;
            multiple.value = val.length > 1;
        };

        const handleBatchDelete = () => {
            if (selectedRows.value.length == 0) {
                $message.error('请选择要删除的数据');
                return;
            }
            var ids = selectedRows.value.map(item => item.id).join(',');
            handleDelete(ids);
        };

        const handleAddClassify = () => {
            $refs.categoryAddForm.add({});
        };

        const getClassifyAllList = (type, callback) => {
            let params = { classifyType: type };
            getClassifyAllList(params).then(res => {
                if (res.code == 200) {
                    classifyList.value = res.data;
                    callback && callback(res.data);
                } else {
                    $message.error(res.msg);
                }
            });
        };

        const generatePrintData = async () => {
            const Cols = columns.value.map(v => {
                let item = {
                    name: v.dataIndex,
                    required: false,
                    size: 255,
                    type: "str",
                };
                return item;
            });

            const data = dataSource.value.map(v => {
                let item = {};
                columns.value.forEach(val => {
                    item[val.dataIndex] = v[val.dataIndex];
                });
                return item;
            });

            let table = templateData;
            table.Tables[0] = {
                Cols: Cols,
                Data: data,
                Name: 'table'
            };

            let ColHead = [], dataHead = [];

            for (let key in model) {
                ColHead.push({
                    name: key,
                    required: false,
                    size: 255,
                    type: "str",
                });
            }

            table.Tables[1] = {
                Cols: ColHead,
                Data: [model],
                Name: 'manInfo'
            };

            table.upload_templ_comment = title;
            table.upload_templ_name = title;
            table.upload_templ_user = model.loginName || model.loginName;
            table.template = `${fileName[billType]}.fr3`;

            console.log('table :>> ', table);
            console.log('fileName :>> ', fileName);
            sendMsg(table, undefined, getPrintAuth);
        };

        onMounted(() => {
            getList();
        });

        return {
            dataSource,
            multiple,
            loading,
            showSearch,
            queryParams,
            total,
            selectedRows,
            categoryList,
            classifyList,
            codeToText,
            handleAdd,
            handleEdit,
            handleQuery,
            resetQuery,
            updateDateRange,
            dateRange,
            getList,
            getQueryParams,
            handleDelete,
            selectHandle,
            handleSelectionChange,
            handleBatchDelete,
            handleAddClassify,
            getClassifyAllList,
            generatePrintData
        };
};
